package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 统计指定日期营业数据
     *
     * @return
     */
    public BusinessDataVO businessData(LocalDate now) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        //1.查询营业额，有效订单数，新增用户数
        Map map = new HashMap();
        map.put("time", now);

        //1.1新增用户：select count(*) from user where create_time =？
        Integer newUsers = userMapper.selectByMap(map);

        //1.2查询总订单数：select count(*) from orders where order_time =？
        Integer totalOrderCount = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //1.3查询营业额：select sum(amount) from where order_time =？and status =5
        Double turnover = orderMapper.sumByMap(map);
        //1.4有效订单：select count(*) from orders where order_time =？and status =5
        Integer validOrderCount = orderMapper.countByMap(map);

        //2.计算订单完成率,平均客客单价
        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;

        if (totalOrderCount != 0) {
            //计算订单完成率：有效订单/总订单数
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
        }
        if (validOrderCount != 0) {
            //平均客单价：营业额/有效订单
            unitPrice = turnover / validOrderCount.doubleValue();
        }

        //3.封装查询结果
        return BusinessDataVO.builder()
                .turnover(turnover)//营业额
                .validOrderCount(validOrderCount)//有效订单
                .orderCompletionRate(orderCompletionRate)//订单完成率
                .unitPrice(unitPrice) //平均客单价
                .newUsers(newUsers)//新增用户
                .build();
    }


    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO getOrderOverView(LocalDate now) {
        Map map = new HashMap();
        map.put("time", now);

        //待接单：2
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        //待派送：4
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //已完成:5
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //已取消：6
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders) //待接单数量
                .deliveredOrders(deliveredOrders) //待派送数量
                .completedOrders(completedOrders) //已完成数量
                .cancelledOrders(cancelledOrders) //已取消数量
                .allOrders(allOrders) //全部订单
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        //查询不同状态的菜品数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        //封装/返回查询结果
        return DishOverViewVO.builder()
                .sold(sold) //已起售数量
                .discontinued(discontinued) //已停售数量
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        //查询不同状态的套餐数量
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);

        //封装/返回查询结果
        return SetmealOverViewVO.builder()
                .sold(sold) //已起售数量
                .discontinued(discontinued) //已停售数量
                .build();
    }
}
